[Python] 之旅第10天
實用練習
練習1- 公司請假系統能計算分鐘數差,並且系統能區別週六週日,午休的時間不算
import datetime
from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date, datetime, time ,timedelta
app = Flask(__name__)
@app.route('/data')
def query_example():
try: # try,expect 用來捕捉錯誤資訊
date1 = request.args['date1'] # 能從URL捕捉"date1 = "後頭我們寫入的資訊,並存在date1
date2 = request.args['date2'] # 能從URL捕捉"date2 = "後頭我們寫入的資訊,並存在date2
# 把只是string 的資料格式變成電腦能識別的時間格式
# date1 原本只是string, date1_in_system 是時間格式 time.data
date1_in_system = datetime.strptime( date1, "%Y-%m-%d|%H:%M:%S")
date2_in_system = datetime.strptime( date2, "%Y-%m-%d|%H:%M:%S")
diff = date2_in_system - date1_in_system
dif_in_second = diff.total_seconds() #把時間差轉成秒數差
dif_in_minute = dif_in_second / 60 #秒數差除60 = 分鐘
status = 0
limit_minutes = 2880 #設定請假不能超過兩天的2880 分鐘
if dif_in_minute > limit_minutes:
status = "失敗,請超過兩天2880 分鐘 "
if dif_in_minute <= limit_minutes:
status = "可請假 "
except Exception as e:
return '''
<h1>捕捉錯誤資訊: {}</h1>
'''.format(date1+ str(e))
return '''
<h1>The date1_in_system is: {}</h1>
<h1>The date2_in_system is: {}</h1>
<h1>The diff is: {}</h1>
<h1>The dif_in_second is: {}</h1>
<h1>The dif_in_minute is: {}</h1>
<h1>The 請假狀態是: {}</h1>
'''.format(date1_in_system,date2_in_system, diff, dif_in_second, dif_in_minute,status )
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)
成果會長這樣喔:
公式- 日期,時間的相差概念
成果會長這樣喔:
其他貼文~~
(變強,就從小小的累積開始)
[Python] 之旅第1天 - python環境
https://ithelp.ithome.com.tw/articles/10296280
[Python] 之旅第2天-用 [Python] 跟 [Flask] 為基礎,把 URL input 抓出來
https://ithelp.ithome.com.tw/articles/10296290
[Python] 之旅第3天- 用 [Python] 跟 [Flask]
https://ithelp.ithome.com.tw/articles/10296965
[Python] 之旅第4天- [Python] [Flask] 的應用
https://ithelp.ithome.com.tw/articles/10303948
[Python] 之旅第5天- [Python] [Flask] 的應用, 日期計算 及日期報錯篇
https://ithelp.ithome.com.tw/articles/10309202
[Python] 之旅第6天- [Python] [Flask] 的應用, 比較有小數點圓的面積大小,簡易請假系統製作
https://ithelp.ithome.com.tw/articles/10309352
[Python] 之旅第7天- [Python] [regular expression] 的應用,找特定資料,或限制輸入的格式
https://ithelp.ithome.com.tw/articles/10309371
[Python] 之旅第8天 - [Python] [regular expression] 的應用, 甚麼是 Json
https://ithelp.ithome.com.tw/articles/10309422
[Python] 之旅第9天 - [Python] [Json] 進階說明
https://ithelp.ithome.com.tw/articles/10309620
[Python] 之旅第10天 - [Python] [Flask] 承DAY6 請假系統延伸
https://ithelp.ithome.com.tw/articles/10309883